using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Web;
using System.Web.UI.WebControls;
using System.Reflection;

using Pampa.CRUD.UI.CustomControls;

namespace Pampa.CRUD.Configuration
{
    /// <summary>
    /// Esta clase representa la definicion de un item a editar o visualizar en el CRUD de una entidad
    /// </summary>
    public abstract class VisibleItem : Item
    {
        string label;
        int labelWidth;
        int valueWidth;
        string permissionRequired;
        bool enabled;

        List<ItemLink> links = new List<ItemLink>();

        public VisibleItem() : base()
        {
            this.LabelWidth = 0;   // ancho por defecto
            this.ValueWidth = 200; // ancho por defecto
        }

        public VisibleItem(VisibleItem sourceItem) : base(sourceItem)
        {
            this.Label = sourceItem.Label;
            this.LabelWidth = sourceItem.LabelWidth;
            this.ValueWidth = sourceItem.ValueWidth;
            this.PermissionRequired = sourceItem.PermissionRequired;
            this.Enabled = sourceItem.Enabled;
        }
        
        #region Properties

        public string Label
        {
            get { return label; }
            set { label = value; }
        }

        public int LabelWidth
        {
            get { return labelWidth; }
            set { labelWidth = value; }
        }

        public int ValueWidth
        {
            get { return valueWidth; }
            set { valueWidth = value; }
        }

        public string PermissionRequired
        {
            get { return permissionRequired; }
            set { permissionRequired = value; }
        }

        public bool Enabled
        {
            get 
            { 
                return enabled; 
            }
            set 
            { 
                enabled = value;

                foreach (ItemLink itemLink in this.Links)
                {
                    itemLink.Enabled = enabled;
                }
            }
        }

        public List<ItemLink> Links
        {
            get { return links; }
        }
        
        #endregion

        #region Abstract

        public abstract VisibleItem Clone();

        public abstract ItemType ItemType { get; }

        #endregion

        #region Public Functions

        public Label GetCaption()
        {
            Label label = new Label();
            
            if (this.LabelWidth != 0)
                label.Width = this.LabelWidth;

            label.Text = this.Label;
            label.CssClass = "caption";

            return label;
        }

        public override void LoadXml(XmlNode node)
        {
            try
            {
                base.LoadXml(node);

                if (node.Attributes["Label"] != null)
                    this.Label = node.Attributes["Label"].Value;
                if (node.Attributes["LabelWidth"] != null)
                    this.LabelWidth = int.Parse(node.Attributes["LabelWidth"].Value);
                if (node.Attributes["ValueWidth"] != null)
                    this.ValueWidth = int.Parse(node.Attributes["ValueWidth"].Value);
                if (node.Attributes["PermissionRequired"] != null)
                    this.PermissionRequired = node.Attributes["PermissionRequired"].Value;

                XmlNode nodeLinks = ConfigurationManager.GetChildNode(node, "ItemLinks");

                if (nodeLinks != null)
                {
                    foreach (XmlNode childNode in nodeLinks.ChildNodes)
                    {
                        ItemLink link = new ItemLink();
                        link.LoadXml(childNode);
                        links.Add(link);
                    }
                }   
            }
            catch (Exception ex)
            {
                throw new Pampa.CRUD.Exceptions.ConfigurationException("Item " + this.Id + " mal definido.", ex);
            }
        }

        #endregion
    }
}